﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MySql.Data.MySqlClient;
using System.Data.SqlClient;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace App_Code
{
    /// <summary>
    /// Summary description for AddUsers
    /// </summary>
    public class AddUsers
    {

        static readonly string _connStr = String.Format("server={0};uid={1};pwd={2};database={3};Allow User Variables=True;",
                                                     "anotherurl.com", "root", "rootpw!", "laostraveller");

        public void ExecuteInsert(string username, string password, string name, string lname, string web, string cname, string street, string province, string country)
        {
            try
            {
                string sql = "INSERT INTO tblusers (UserName, Password, Name, Lname, Web, Company, Street, Province, Country) VALUES (@UserName,@Password,@Name,@Lname,@Web, @Company, @Street, @Province, @Country)";
                using (var conn = new MySqlConnection(_connStr))
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand(sql);
                    SqlParameter[] param = new SqlParameter[9];

                    param[0] = new SqlParameter("@UserName", SqlDbType.VarChar, 50);

                    param[1] = new SqlParameter("@Password", SqlDbType.VarChar, 50);

                    param[2] = new SqlParameter("@Name", SqlDbType.VarChar, 50);

                    param[3] = new SqlParameter("@Lname", SqlDbType.Char, 50);

                    param[4] = new SqlParameter("@Web", SqlDbType.Int, 100);

                    param[5] = new SqlParameter("@Company", SqlDbType.VarChar, 50);

                    param[6] = new SqlParameter("@Street", SqlDbType.VarChar, 50);

                    param[7] = new SqlParameter("@Province", SqlDbType.VarChar, 50);

                    param[8] = new SqlParameter("@Country", SqlDbType.VarChar, 50);

                    param[0].Value = username;

                    param[1].Value = password;

                    param[2].Value = name;

                    param[3].Value = lname;

                    param[4].Value = web;

                    param[5].Value = cname;

                    param[6].Value = street;

                    param[7].Value = province;

                    param[8].Value = country;

                    for (int i = 0; i < param.Length; i++)
                    {

                        cmd.Parameters.Add(param[i]);

                    }



                    cmd.CommandType = CommandType.Text;

                    cmd.ExecuteNonQuery();
                    conn.Close();
                }

            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                string msg = "Insert Error:";

                msg += ex.Message;

                throw new Exception(msg);
            }





        }
        /*
        protected void Button1_Click(object sender, EventArgs e)
        {

            if (TxtPassword.Text == TxtRePassword.Text)
            {

                //call the method to execute insert to the database

                ExecuteInsert(TxtName.Text, TxtUserName.Text, TxtPassword.Text, DropDownList1.SelectedItem.Text, TxtAge.Text, TxtAddress.Text);



                Response.Write("Record was successfully added!");



                ClearControls(Page);

            }

            else
            {

                Response.Write("Password did not match");

                TxtPassword.Focus();

            }

        }
         */
        public static void ClearControls(Control Parent)
        {

            if (Parent is TextBox)

            { (Parent as TextBox).Text = string.Empty; }

            else
            {

                foreach (Control c in Parent.Controls)

                    ClearControls(c);

            }

        }

        public void SubmitReport(string p, string p_2)
        {
            throw new NotImplementedException();
        }

       
    }

    
}
